package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PlayRecordAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.ActivityPlayRecordBinding;
import flc.ast.utils.AppSpUtils;

public class PlayRecordActivity extends BaseAc<ActivityPlayRecordBinding> {

    private List<RecordBean> listShow = new ArrayList<>();
    private PlayRecordAdapter recordAdapter;
    private Dialog myDeleteDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_play_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPlayRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlayRecordClear.setOnClickListener(this);
        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recordAdapter = new PlayRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        //清空弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlayRecordClear:
                myDeleteDialog.show();
                break;
            case R.id.tvDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogDeleteRight:
                listShow.clear();
                AppSpUtils.savePlayRecordList(listShow);
                getData();
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        List<RecordBean> listSp = AppSpUtils.getPlayRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivPlayRecordNoData.setVisibility(View.GONE);
            mDataBinding.ivPlayRecordClear.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivPlayRecordClear.setVisibility(View.GONE);
            mDataBinding.ivPlayRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.85);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView tip=inflate.findViewById(R.id.tvDialogDeleteTip);
        TextView cancel = inflate.findViewById(R.id.tvDialogDeleteCancel);
        TextView right = inflate.findViewById(R.id.tvDialogDeleteRight);
        tip.setText(getString(R.string.delete_play_record));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}